const Path = require('path');
const Cp = require('child_process');

function getGitRevision() {
    const gitContent = {
        version: Cp.execSync('git describe --tags --always', { encoding: 'utf8' }),
        commithash: Cp.execSync('git rev-list --max-count=1 --no-merges HEAD', {
            encoding: 'utf8'
        }),
        branch: Cp.execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf8' })
    };
    return gitContent;
}

const config = {
    projectName: 'taro',
    date: '2020-11-12',
    designWidth: 750,
    deviceRatio: {
        640: 2.34 / 2,
        750: 1,
        828: 1.81 / 2
    },
    sourceRoot: 'src',
    outputRoot: 'dist',
    plugins: [],
    defineConstants: {
        VERSION: JSON.stringify(getGitRevision().version),
        COMMITHASH: JSON.stringify(getGitRevision().commithash),
        BRANCH: JSON.stringify(getGitRevision().branch),
        BUILDDATE: JSON.stringify(new Date())
    },
    copy: {
        patterns: [
            {
                from: 'src/components/vant/wxs/',
                to: 'dist/components/vant/wxs/'
            }
        ],
        options: {}
    },
    sass: {
        resource: [
            'src/assets/styles/basic/_variables.scss',
            'src/assets/styles/basic/_mixins.scss'
        ],
        projectDirectory: Path.resolve(__dirname, '..')
    },
    alias: {
        '@': Path.resolve(__dirname, '..', 'src')
    },
    framework: 'react',
    mini: {
        postcss: {
            pxtransform: {
                enable: true,
                config: {}
            },
            url: {
                enable: true,
                config: {
                    limit: 1024
                }
            },
            cssModules: {
                enable: false,
                config: {
                    namingPattern: 'module',
                    generateScopedName: '[name]__[local]___[hash:base64:5]'
                }
            }
        }
    }
};

module.exports = merge => {
    if (process.env.NODE_ENV === 'development') {
        return merge({}, config, require('./dev'));
    }
    return merge({}, config, require('./prod'));
};
